#define _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int maxn = 1e6 + 10;
vector<int> fb(maxn);

int main() {
    int n;
    while (cin >> n) {
        fb[0] = 0, fb[1] = 1;
        int i = 2;
        for (; fb[i - 2] + fb[i - 1] <= 1e7; i++)
            fb[i] = fb[i - 2] + fb[i - 1];

        auto it = upper_bound(&fb[0], &fb[i], n);
        int ans = min(*it - n, n - *(it - 1));
        cout << ans << endl;
    }
}
